package com.usian.wemedia.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.usian.common.dtas.BaseServiceImpl;
import com.usian.common.fastdfs.FastDFSClient;
import com.usian.model.common.dtos.PageResponseResult;
import com.usian.model.common.dtos.ResponseResult;
import com.usian.model.common.enums.AppHttpCodeEnum;
import com.usian.model.media.dtos.WmMaterialDto;
import com.usian.model.media.pojos.WmMaterial;
import com.usian.model.media.pojos.WmNewsMaterial;
import com.usian.utils.threadlocal.WmThreadLocalUtils;
import com.usian.wemedia.mapper.WmMaterialMapper;
import com.usian.wemedia.mapper.WmNewsMaterialMapper;
import com.usian.wemedia.service.MaterialManageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class MaterialManageServiceImpl extends BaseServiceImpl<WmMaterialMapper, WmMaterial> implements MaterialManageService {
    @Value("${fdfs.url}")
    private String fsdfsurl;
    @Autowired
    private FastDFSClient fastDFSClient;

    @Override
    public ResponseResult findList(WmMaterialDto dto) {
        if (dto == null) return ErrorParam;
        Page page = new Page(dto.getPage(), dto.getSize());
        //根据用户id查询素材列表
        LambdaQueryWrapper<WmMaterial> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(dto.getIsCollection() != null, WmMaterial::getIsCollection, dto.getIsCollection());
        Integer id = WmThreadLocalUtils.getUser().getId();
        wrapper.eq(WmMaterial::getUserId, id);
        //根据时间倒叙排序
        wrapper.orderByDesc(WmMaterial::getCreatedTime);
        IPage page1 = page(page, wrapper);
        PageResponseResult responseResult = new PageResponseResult(dto.getPage(), dto.getSize(), (int) page1.getTotal());
        responseResult.setData(page1.getRecords());
        List<WmMaterial> data = (List<WmMaterial>) responseResult.getData();
        data.forEach(v -> {
            v.setUrl(fsdfsurl + v.getUrl());
        });
        return ResponseResult.okResult(data);
    }

    @Autowired
    private WmNewsMaterialMapper wmNewsMaterialMapper;

    @Override
    public ResponseResult delPicture(Integer id) {
        //1.检查参数
        if (id == null) return ErrorParam;
        //2.判断当前图片是否引用
        WmMaterial wmMaterial = getById(id);
        if (wmMaterial == null) return ErrorParam;
        LambdaQueryWrapper<WmNewsMaterial> wrapper = new LambdaQueryWrapper<>();
        //3.根据id查找图片引用素材列表
        wrapper.eq(WmNewsMaterial::getMaterialId, id);
        Integer count = wmNewsMaterialMapper.selectCount(wrapper);
        if (count > 0) return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST, "图片已经在用");
        //4.删除fasf中图片
        String replace = wmMaterial.getUrl();
        fastDFSClient.delFile(replace);
        removeById(id);
        return Success;
    }

    //收藏
    @Override
    public ResponseResult cancleCollectionMaterial(Integer id, Short type) {
        if (id == null) return ErrorParam;
        LambdaUpdateWrapper<WmMaterial> wrapper = new LambdaUpdateWrapper<>();
        update(wrapper.set(WmMaterial::getIsCollection, type).eq(WmMaterial::getUserId, WmThreadLocalUtils.getUser().getId()).eq(WmMaterial::getId, id));
        return Success;
    }

    //取消收藏
    @Override
    public ResponseResult collectionMaterial(Integer id, Short type) {
        if (id==null)return ErrorParam;
        if (type!=0)return ErrorParam;
        LambdaUpdateWrapper<WmMaterial> wrapper = new LambdaUpdateWrapper<>();
        update(wrapper.set(WmMaterial::getIsCollection,type).eq(WmMaterial::getUserId,WmThreadLocalUtils.getUser().getId()).eq(WmMaterial::getId,id));
        return Success;
    }
}
